const dbpool = require('../config/dbpoolconfig');

const userController = {
    // 登录
    login(req, res) {
        let username = req.body.username || req.query.username;
        let pwd = req.body.pwd || req.query.pwd;
        dbpool.connect('select * from account where username=? and pwd=?', [username, pwd], (err, data) => {
            if (!err) {
                if (data.length > 0) {
                    res.send('登录成功')
                } else {
                    res.send('登录失败')
                }
            }
        })
    },
    // 用户列表
    userList(req, res) {
        dbpool.connect('select * from userlist', [], (err, data) => {
            if (!err) {
                res.send(data);
            }
        })
    },
    // 搜索
    userListSearch(req, res) {
        let searchName = req.query.searchName || '';
        let searchNum = req.query.searchNum || '';
        let name = req.query.name || '';
        let selectResult = req.query.selectResult || '';
        let dateResult = req.query.dateResult || '';


        let sql = 'select * from userlist where 1=1 ';
        let params = [];

        if (searchNum.trim() != '') {
            sql += 'and ' + searchName + ' like ? ';
            searchNum = '%' + searchNum.trim() + '%';
            params.push(searchNum)
        }

        if (name.trim() != '') {
            sql += 'and name like ?';
            name = '%' + name.trim() + '%';
            params.push(name)
        }

        if (selectResult != '') {
            for (let key in selectResult) {
                if (selectResult[key] != '全部') {
                    sql += 'and ' + key + ' = ? ';
                    params.push(selectResult[key])
                }
            }
        }

        if (dateResult != '') {
            for (let key in dateResult) {
                if (dateResult[key] != '') {
                    let range = dateResult[key].split('~');
                    sql += 'and ' + key + ' between ? and ? ';
                    params.push(range[0], range[1])
                }

            }
        }

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                res.send(data);
            }
        })
    },
    // 添加
    addUser(req, res) {
        let addUserData = req.body.addUserData || req.query.addUserData;
        // 获取当前日期
        let date = new Date();
        let year = date.getFullYear();
        let month = (date.getMonth() + 1).toString().padStart(2, 0);
        let day = date.getDate().toString().padStart(2, 0);

        // 判断注册时间是否为当前日期
        let thisDate = year + '-' + month + '-' + day;
        let loginDate = addUserData.registerDate.split(' ');

        if (addUserData['sex'] == '0') {
            addUserData['sex'] = '男'
        } else {
            addUserData['sex'] = '女'
        }
        if (addUserData['mode'] == '2') {
            addUserData['mode'] = '正常'
            addUserData['operate'] = '冻结'
        } else {
            addUserData['mode'] = '已冻结'
            addUserData['operate'] = '解冻'
        }
        addUserData['vip'] = 'VIP1'
        addUserData['loginDate'] = `${addUserData.registerDate}`
        if (loginDate[0] == thisDate) {
            addUserData['online'] = '当前在线'
        } else {
            addUserData['online'] = '离线'
        }
        addUserData['IP'] = '192.168.03'
        addUserData['wallet'] = '0'

        let params = [null]
        for (let key in addUserData) {
            params.push('' + addUserData[key] + '')
        }

        dbpool.connect('insert into userlist values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
            params,
            (err, data) => {
                console.log(err);
                console.log(data);
                if (!err) {
                    res.send('success')
                }
            })

        console.log(addUserData);
    },
    // 批量删除
    delSelectionUser(req, res) {
        let delSelection = req.query.selectionUser || '';
        let sql = 'delete from userlist where id = ? ';
        let params = [];
        if (delSelection != '' || delSelection != []) {
            if (delSelection.length > 1) {
                for (let i = 1; i < delSelection.length; i++) {
                    sql += 'or id = ? '
                }
                delSelection.forEach(item => {
                    params.push(item)
                })
            } else {
                delSelection.forEach(item => {
                    params.push(item)
                })
            }
            dbpool.connect(sql, params, (err, data) => {
                if (!err) {
                    res.send('success')
                }
            })
        } else {
            res.send('null')
        }
    },
    // 修改状态
    alterMode(req, res) {
        let id = req.query.userID || req.body.userID;
        let mode = req.query.userMode || req.body.userMode;
        let operate = req.query.userOperate || req.body.userOperate;

        let params = []

        if (mode == '正常') {
            mode = '已冻结'
            operate = '解冻'
            params.push(mode, operate, id)
        } else {
            mode = '正常'
            operate = '冻结'
            params.push(mode, operate, id)
        }
        dbpool.connect('UPDATE userlist SET mode = ?,operate=? WHERE id = ?', params, (err, data) => {
            if (!err) {
                dbpool.connect('update userrecord INNER JOIN userlist ON userlist.id = userrecord.reportedId set userrecord.reportedMode = userlist.mode ,userrecord.operate = userlist.operate',[],(err,data)=>{
                    if(!err){
                        res.send('success')
                    }
                })
            }
        })
    },
    // 修改信息
    alterUser(req, res) {
        let updateData = req.query.alterData || req.body.alterData;
        if (updateData.sex == 0) {
            updateData.sex = '男'
        } else {
            updateData.sex = '女'
        }
        if (updateData.mode == 2) {
            updateData.mode = '正常',
                updateData.operate = '冻结'
        } else {
            updateData.mode = '已冻结',
                updateData.operate = '解冻'
        }

        let sql = 'UPDATE userlist SET img = ?,name=?,tel=?,chatNum=?,sex=?,palNum=?,groupNum=?,registerDate=?,mode=?,operate=? WHERE id = ?'
        let params = [updateData.img, updateData.name, updateData.tel, updateData.chatNum, updateData.sex, updateData.palNum, updateData.groupNum, updateData.registerDate, updateData.mode, updateData.operate, updateData.id]

        dbpool.connect(sql, params, (err, data) => {
            console.log(err);
            console.log(data);
            if (!err) {
                res.send('success')
            }
        })
    },
    // 删除该用户
    delUser(req, res) {
        let id = req.query.userID || req.body.userID;
        dbpool.connect('delete from userlist where id = ?', [id], (err, data) => {
            if (!err) {
                res.send('success')
            }
        })
    },

    // 用户私信
    chatRecordList(req, res) {
        dbpool.connect('select * from chatrecordlist', [], (err, data) => {
            if (!err) {
                res.send(data)
            }
        })
    },
    // 搜索
    chatRecordListSearch(req, res) {
        let sendId = req.query.sendID || ''
        let sendName = req.query.sendName || ''
        let receiveId = req.query.receiveID || ''
        let receiveName = req.query.receiveName || ''
        let sendContent = req.query.sendContent || ''

        let sql = 'select * from chatrecordlist where 1=1 '
        let params = [];

        if (sendId.trim() != '') {
            sql += 'and sendId like ? ';
            sendId = '%' + sendId.trim() + '%';
            params.push(sendId)
        }
        if (sendName.trim() != '') {
            sql += 'and sendName like ? ';
            sendName = '%' + sendName.trim() + '%';
            params.push(sendName)
        }
        if (receiveId.trim() != '') {
            sql += 'and receiveId like ? ';
            receiveId = '%' + receiveId.trim() + '%';
            params.push(receiveId)
        }
        if (receiveName.trim() != '') {
            sql += 'and receiveName like ? ';
            receiveName = '%' + receiveName.trim() + '%';
            params.push(receiveName)
        }
        if (sendContent.trim() != '') {
            sql += 'and sendContent like ? ';
            sendContent = '%' + sendContent.trim() + '%';
            params.push(sendContent)
        }

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                res.send(data)
            }
        })
    },
    // 批量删除
    delSelectionChatRecord(req, res) {
        let delSelection = req.query.delSelectionData || ''
        let sql = 'delete from chatrecordlist where sendId = ? '
        let params = []
        if (delSelection != '' || delSelection != []) {
            if (delSelection.length > 1) {
                for (let i = 1; i < delSelection.length; i++) {
                    sql += 'or sendId = ? '
                }
                delSelection.forEach(item => {
                    params.push(item)
                })
            } else {
                delSelection.forEach(item => {
                    params.push(item)
                })
            }
            dbpool.connect(sql, params, (err, data) => {
                if (!err) {
                    res.send('success')
                }
            })
        } else {
            res.send('null')
        }

    },
    // 删除当前行
    delRow(req, res) {
        let sendId = req.query.delRow || '';
        if (sendId != '') {
            dbpool.connect('delete from chatrecordlist where sendId = ?', [sendId], (err, data) => {
                if (!err) {
                    res.send('success')
                }
            })
        }
    },

    // 用户IP
    userIPSearch(req, res) {
        let searchName = req.query.searchName || ''
        let searchNum = req.query.searchNum || ''
        let name = req.query.toName || ''
        let IP = req.query.IP || ''
        let loginDate = req.query.loginDate || ''
        let mode = req.query.selectValue || ''

        let sql = 'select * from userlist where 1=1 '
        let params = []

        if (searchNum.trim() != '') {
            sql += 'and ' + searchName + ' like ? '
            searchNum = '%' + searchNum.trim() + '%';
            params.push(searchNum)
        }
        if (name.trim() != '') {
            sql += 'and name like ? '
            name = '%' + name.trim() + '%';
            params.push(name)
        }
        if (IP.trim() != '') {
            sql += 'and IP like ? '
            IP = '%' + IP.trim() + '%';
            params.push(IP)
        }
        if (loginDate != '') {
            sql += 'and loginDate between ? and ? ';
            params.push(loginDate[0], loginDate[1])
        }
        if (mode != '全部') {
            sql += 'and mode = ?';
            params.push(mode)
        }

        dbpool.connect(sql,params,(err,data)=>{
            if(!err){
                res.send(data)
            }
        })
    },
    // 举报记录
    userRecord(req,res){
        dbpool.connect('select * from userrecord',[],(err,data)=>{
            if(!err){
                res.send(data)
            }
        } )
    },
    // 搜索
    userRecordSearch(req,res){
        let reportID= req.query.reportID || '';
        let reportName= req.query.reportName || '';
        let reportedID= req.query.reportedID || '';
        let reportedName= req.query.reportedName || '';
        let reportedContent= req.query.reportedContent || '';
        let reportedMode= req.query.selectValue || '';
        let reportDate= req.query.reportDate || '';

        let sql = 'select * from userrecord where 1=1 '
        let params = [];

        if(reportID.trim() != ''){
            sql += 'and reportID like ? ',
            reportID = '%'+reportID.trim()+'%'
            params.push(reportID)
        }
        if(reportName.trim() != ''){
            sql += 'and reportName like ? ',
            reportName = '%'+reportName.trim()+'%'
            params.push(reportName)
        }
        if(reportedID.trim() != ''){
            sql += 'and reportedId like ? ',
            reportedID = '%'+reportedID.trim()+'%'
            params.push(reportedID)
        }
        if(reportedName.trim() != ''){
            sql += 'and reportedName like ? ',
            reportedName = '%'+reportedName.trim()+'%'
            params.push(reportedName)
        }
        if(reportedContent != '' && reportedContent != '全部'){
            sql += 'and reportedContent = ? ',
            params.push(reportedContent)
        }
        if(reportedMode != '' && reportedMode != '全部'){
            sql += 'and reportedMode = ? ',
            params.push(reportedMode)
        }
        if(reportDate != ''){
            sql += 'and reportDate between ? and ? ',
            params.push(reportDate[0],reportDate[1])
        }

        dbpool.connect(sql,params,(err,data)=>{
            if(!err){
                res.send(data)
            }
        })
    },
    // 修改状态
    userRecordMode(req,res){
        let reportedId = req.query.reportedId || req.body.reportedId;
        let reportedMode = req.query.reportedMode || req.body.reportedMode;
        let operate = req.query.operate || req.body.operate;

        let params = []

        if (reportedMode == '正常') {
            reportedMode = '已冻结'
            operate = '解冻'
            params.push(reportedMode, operate, reportedId)
        } else {
            reportedMode = '正常'
            operate = '冻结'
            params.push(reportedMode, operate, reportedId)
        }

        dbpool.connect('UPDATE userrecord SET reportedMode = ?,operate=? WHERE reportedId = ?', params, (err, data) => {
            if (!err) {
                dbpool.connect('update userlist INNER JOIN userrecord ON userlist.id = userrecord.reportedId set userlist.mode = userrecord.reportedMode,userlist.operate = userrecord.operate',[],(err,data)=>{
                    if(!err){
                        res.send('success')
                    }
                })
            }
        })
        
        
    }
}
module.exports = userController;